<template>
  <div>
    <div class="box">
      <div class="login">
        <div class="img"><img src="../assets/logo.png" alt="" /></div>

        <!-- 内容 -->
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              prefix-icon="el-icon-user-solid"
              v-model="ruleForm.username"
            >
            </el-input>
          </el-form-item>
          <!-- 密码 -->

          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              prefix-icon="el-icon-search"
              show-password
              v-model="ruleForm.password"
            >
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >登录</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 内容 -->
      </div>
    </div>
  </div>
</template>

<script>
//引入
import { login } from "@/request/api.js";
export default {
  data() {
    return {
      //登录数据
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      //规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //登录
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.ruleForm).then(res=>{
            console.log(res)
            if(res.status != 200) {
                this.$message.error('登录失败');
            }else {
              localStorage.setItem('token',res.data.data.token)
              this.$message({
                message: "登录成功",
                type: "success",
              });
              this.$router.push('/home')
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {},
  mounted() {},
  components: {},
  computed: {},
  watch: {},
};
</script>

<style lang='scss' scoped>
.box {
  // width: 100vw;
  height: 100vh;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .login {
    position: relative;
    width: 400px;
    height: 300px;
    // border: 1px solid #fff;
    background-color: #fff;
    box-shadow: 0 0 10px #fff;
    .img {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      border: 10px solid #fff;
      position: absolute;
      background-color: #eeeeee;
      left: 140px;
      top: -60px;
      box-shadow: 0 0 10px #fff;
      img {
        width: 100%;
        height: 100%;
        margin-top: 10px;
      }
    }
  }
  .el-form {
    margin: 60px 0 0 -70px;
  }
  .el-form-item {
    width: 90%;
  }
}
</style>

